package org.esky.spring.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class HelloWordAspect {
	
	@Before(value = "execution(* org.esky.spring.aop.IHelloWorld.*(..))")
	public void beforeSayHello(JoinPoint joinPoint) {
		System.out.println("Before : " + joinPoint.getArgs()[0]);
	}
	
	@After(value = "execution(* org.esky.spring.aop.IHelloWorld.*(..)) && args(message)")
	public void afterSayHello(String message) {
		System.out.println("After : " + message);
	}
	
	@Around(value = "execution(* org.esky.spring.aop.IHelloWorld.*(..))")
	public Object aroundSayHello(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("Around Before !");
		Object object = joinPoint.proceed();
		System.out.println("Around After !");
		return object;
	}
	
	@AfterReturning(value = "execution(* org.esky.spring.aop.IHelloWorld.*(..))", returning = "reval")
	public void afterReturningSayHello(Object reval) {
		System.out.println("After Returning : " + reval);
	}
	
	@AfterThrowing(value = "execution(* org.esky.spring.aop.IHelloWorld.*(..))", throwing = "ex")
	public void afterThrowingSayHello(Exception ex) {
		System.out.println("After Throwing : " + ex.getMessage());
	}
}
